const http = require('http')
const url = require('url')
const fs = require('fs')
const game = require('./lib.js')

let count = 0
let previous = 0

http.createServer(function(req, resp) {
    const reqUrl = new URL(req.url, 'http://localhost/')
    
    if (reqUrl.pathname === '/action.do') {
        const playerAction = reqUrl.searchParams.get('name')
        let result = game(playerAction)
        //连赢三次，输出“不玩了！”，后续的请求都响应“不玩了！”
        resp.writeHead(200)
        if (result === 0) {
            resp.end("平局！")
        } else if (result === 1) {
            resp.end("您赢了！")
        } else {
            resp.end("您输了！")
        }
    }

    if (reqUrl.pathname === '/') {
        resp.writeHead(200)
        fs.createReadStream('index.html').pipe(resp)
    }
}).listen(3000)